package com.cnpc.iot.scgl.controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.cnpc.framework.annotation.RefreshCSRFToken;
import com.cnpc.framework.annotation.VerifyCSRFToken;
import com.cnpc.framework.base.pojo.Result;
import com.cnpc.framework.base.service.BaseService;
import com.cnpc.framework.utils.StrUtil;
import com.cnpc.iot.scgl.entity.Pflow;
import com.cnpc.iot.scgl.vo.FlowTree;

/**
* 生产流程管理控制器
* @author jrn
* 2018-03-19 22:07:08由代码生成器自动生成
*/
@Controller
@RequestMapping("/pflow")
public class PflowController {

    @Resource
    private BaseService baseService;
    
    @RequestMapping(value="/getAll", method=RequestMethod.POST)
    @ResponseBody
    public List<Pflow> getAll(){
    	return baseService.find("from Pflow");
    }

    @RequestMapping(value="/list",method = RequestMethod.GET)
    public String list(){
        return "iot/scgl/pflow_list";
    }

    @RefreshCSRFToken
    @RequestMapping(value="/edit",method = RequestMethod.GET)
    public String edit(String id,HttpServletRequest request){
        request.setAttribute("id", id);
        return "iot/scgl/pflow_edit";
    }

    @RequestMapping(value="/detail",method = RequestMethod.GET)
    public String detail(String id,HttpServletRequest request){
        request.setAttribute("id", id);
        return "iot/scgl/pflow_detail";
    }

    @RequestMapping(value="/get/{id}",method = RequestMethod.POST)
    @ResponseBody
    public Pflow get(@PathVariable("id") String id){
        return baseService.get(Pflow.class, id);
    }

    @VerifyCSRFToken
    @RequestMapping(value="/save")
    @ResponseBody
    public Result save(Pflow pflow){
        if(StrUtil.isEmpty(pflow.getId())){
            baseService.save(pflow);
        }
        else{
            pflow.setUpdateDateTime(new Date());
            baseService.update(pflow);
        }
        return new Result(true);
    }

    @RequestMapping(value="/delete/{id}",method = RequestMethod.POST)
    @ResponseBody
    public Result delete(@PathVariable("id") String id){
        Pflow pflow=this.get(id);
        try{
            baseService.delete(pflow);
            return new Result();
        }
        catch(Exception e){
            return new Result(false,"该数据已经被引用，不可删除");
        }
    }

    @RequestMapping(value="/treeData",method = RequestMethod.POST)
    @ResponseBody
    public List<FlowTree> treeData(){
    	List<Pflow> flows = baseService.find("from Pflow");
    	List<FlowTree> ftree = new ArrayList<>();
    	for(Pflow p : flows) {
    		FlowTree tree = new FlowTree();
    		tree.setId(p.getId());
    		tree.setText(p.getName());
    		tree.setLevelCode(p.getCode());
    		ftree.add(tree);
    	}
    	return ftree;
    }

}
